package com.atguigu.eduservice.controller;


import com.atguigu.commonutils.R;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.service.EduChapterService;
import com.atguigu.eduservice.service.EduCourseDescriptionService;
import com.atguigu.eduservice.service.EduCourseService;
import com.atguigu.eduservice.service.EduVideoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author ct_start
 * @since 2021-08-12
 */
@RestController
@RequestMapping("/eduservice/course")
@CrossOrigin
public class EduCourseController {

    // 注入下节和章节的service
    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private EduChapterService eduChapterService;
    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    @Autowired
    private EduCourseService eduCourseService;
    // 课程列表
    // TODO 条件查询带分页功能
    @GetMapping("getAll")
    public R getCourseList(){
        List<EduCourse> list = eduCourseService.list(null);
        return  R.ok().data("list",list);
    }



    // 添加课程基本信息
    @PostMapping("addCourseInfo")
    public R addCourseInfo(@RequestBody CourseInfoVo courseInfoVo){


        // 返回添加之后课程的ID,为了添加大纲使用

        String id=eduCourseService.saveCourseInfo(courseInfoVo);
        return  R.ok().data("courseId",id);
    }

    // 根据课程Id 查询课程的基本信息
    @GetMapping("getCourseInfo/{courseId}")
    public R getCourseInfo(@PathVariable String courseId){

        CourseInfoVo courseInfoVo=eduCourseService.getCourseInfo(courseId);
        return  R.ok().data("courseInfoVo",courseInfoVo);
    }


    // 修改课程信息  根据前台表单传递过来的CourseInfo 进行修改
    @PostMapping("updateCourseInfo")
    public R updateCourseInfo(@RequestBody CourseInfoVo courseInfoVo){
            eduCourseService.updateCourseInfo(courseInfoVo);
            return  R.ok();
    }

    // 根据课程ID 查询课程的确认信息
    @GetMapping("getPublishCourseInfo/{id}")
    public R getPublishCourseInfo(@PathVariable String id){
         CoursePublishVo coursePublishVo =eduCourseService.publishCourseInfo(id);
         return  R.ok().data("publishCourse",coursePublishVo);
    }

    // 课程最终发布状态
    // 修改课程状态

    @PutMapping("publishCourse/{id}")
    public R publishCourse(@PathVariable String id){
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");// 设置课程发布状态
        eduCourseService.updateById(eduCourse);
        return  R.ok();
    }


    // 根据课程Id 删除课程下面所有的章节 小节 视频
    @DeleteMapping("{courseId}")
    public R deleteCourse(@PathVariable String courseId){
        // 删除小节
        eduVideoService.removeChapterByCourseId(courseId);
        // 删除章节
        eduChapterService.removeByCourseId(courseId);

        // 根据id 删除描述
        eduCourseDescriptionService.removeById(courseId);

        // 再删除课程
         eduCourseService.removeById(courseId);

        return  R.ok();
    }
}

